Typed files
Typed files or data files used to store homogeneous data such as bytes, integers,
records, user-defiened types, and other Object Pascal types.
Typed files in Object Pascal
Typed files in Object Pascal declaration depends on record type but the main structure
of declaration is:
var
F: file of ;
Type can be any object Pascal type but not Object such as:
var
IntF: file of integer;
CharF: file of char;
RecF: file of TRec;
The most important procedures and functions used with typed files are below:
---------------------------------------------------------------------------------------------------------------------------------------------------------
Operation proc/func
---------------------------------------------------------------------------------------------------------------------------------------------------------
Declaration var F: file of ;
Assigning AssignFile(F, AFileName);
Opening for reading Reset(F); or Rewrite(F);
Opening for writing Rewrite(F) or Reset(F) with FileMode = 2
Opening for read/write Reset(F) with FileMode = 2
Appending FileMode:= 2; Reset(F); Seek(F, FileSize(F));
Reading Read(F, X); { X is a variable of same file
type }
Writing Writ(F, X);
Goto random position Seek(F, Position); { Position is a number of desired
record }
Closing file CloseFile(F);
Getting records count FileSize(F);
Getting current position FilePos(F);
---------------------------------------------------------------------------------------------------------------------------------------------------------
Typed file is organized as random access file so that you can open it in reading/writing
mode and you can goto any record directly using Seek procedure.
Reset in typed file opens file for reading, writing, or reading and writing mode
according to FileMode variable:
If FileMode = 0 that means read only
If FileMode = 1 that means write only
If FileMode = 2 that means read and write access
Befor openning file with reset make sure that the file is exists.
Rewrite in typed file create a new file and it overwrite the file if it is already
exists. When you create file with Rewrite you can read and write from and to file.
Append does not work with typed files, insead you must open file with reset and
goto beyond last record such as:
FileMode:= 2; // Read and write mode
Reset(F);
Seek(F, FileSize(F));
If file size if 5 that means there is 5 records in file from 0 to 4 so that going
to record number 5 will ensures writing on an empty record. This works only if file
exists but it will rais an exception if file does not exists, so that you must write:
AssignFile(F, FileName);
FileMode:= 2; // Read and write mode
if FileExists(FileName) then
Reset(F)
else
Rewrite(F); // Create new file
Seek(F, FileSize(F));
Example:
(********* Save data **********)
procedure SaveData;
var
F: file of integer;
Data: integer;
begin
AssignFile(F, 'Test.Dat');
Rewrite(F);
Repeat
Data:= StrToInt(InputBox('Data entry',
'Enter any numbers, enter 0 to stop data entry', ''));
Write(F, Data);
until Data = 0;
CloseFile(F);
end;
(********* Read data **********)
procedure ReadData;
var
F: file of integer;
Data: integer;
begin
AssignFile(F, 'Test.Dat');
FileMode:= 0; // Read only
Reset(F);
while not Eof(F) do
begin
Read(F, Data);
ShowMessage(IntToStr(Data));
end; // while
CloseFile(F);
end;
Notes:
- If you want to open typed file for reading you must set FileMode to 0 because
if you do not assign 0 to FileMode the default value of it will be 2 which means
read and write access. If you open a read-only file or a file in CD-ROM and you want
to read it, openning it with reset means read and write if FileMode = 2 which will
generates access denied error and prevent you from reading file.
- Records in typed files have a fixed length, so that it does not require any seperator,
example if you write three records in a typed file and record size is 10 bytes, actual
file size will be 30.
- You can not write long string in typed file, instead you have to determine it's
size such as:
type
TLine = String[30];
var
F: file of TLine;
Line: TLine;
See also:
Text files
Untyped files